/*
 * @Author: cooper
 * @Date: 2025-03-30 21:35:19
 * @Wechat: cmd_develop
 * @Description: EEPROM AT24C128:128K/64 = 2k page,所以有2千页
 * EEPROM存储分布：
 * 0-9： 读取机械臂数据.1个位置点,XYZ3个轴.每个轴占用4个字节.所以每页只能存入 64/(3*4) = 5个数据;
 *       K型机，有36个数据,所以要8页.为了后续扩展，让其占用10页
 * 10：deviceNO,有19个数据,让它存入第10页
 * 11-15: 电机的速度(2)、加速度(2)、报错提示次数(2),机械臂5个电机(5*6=30)，
 * 16:升降机(6+4+4+4+4 = 22)
 * 17:补货门电机(6 +1个打开距离4=10)
 * 18-21：肠盒4个电机
 *
 */
#include "./bsp_eeprom.h"

i2c_handle_type hi2cx;
/// @brief I2C初始化
/// @param hi2c
void i2c_lowlevel_init(i2c_handle_type *hi2c)
{
    gpio_init_type gpio_init_structure;

    if (hi2c->i2cx == I2Cx_PORT)
    {
        /* i2c periph clock enable */
        crm_periph_clock_enable(I2Cx_CLK, TRUE);
        crm_periph_clock_enable(I2Cx_SCL_GPIO_CLK, TRUE);
        crm_periph_clock_enable(I2Cx_SDA_GPIO_CLK, TRUE);

        /* gpio configuration */
        gpio_pin_mux_config(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_GPIO_PinsSource, I2Cx_SCL_GPIO_MUX);

        gpio_pin_mux_config(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_GPIO_PinsSource, I2Cx_SDA_GPIO_MUX);

        /* configure i2c pins: scl */
        gpio_init_structure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
        gpio_init_structure.gpio_mode = GPIO_MODE_MUX;
        gpio_init_structure.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
        gpio_init_structure.gpio_pull = GPIO_PULL_UP;

        gpio_init_structure.gpio_pins = I2Cx_SCL_GPIO_PIN;
        gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_init_structure);

        /* configure i2c pins: sda */
        gpio_init_structure.gpio_pins = I2Cx_SDA_GPIO_PIN;
        gpio_init(I2Cx_SDA_GPIO_PORT, &gpio_init_structure);

        /* config i2c */
        i2c_init(hi2c->i2cx, 0x0F, I2Cx_CLKCTRL);

        i2c_own_address1_set(hi2c->i2cx, I2C_ADDRESS_MODE_7BIT, I2Cx_ADDRESS);
    }
}

/// @brief
/// @param
void eeprom_init(void)
{
    hi2cx.i2cx = I2Cx_PORT;
    /* i2c config */
    i2c_config(&hi2cx);
}

/// @brief 写入EEPROM
/// @param page
/// @param debug_info 是否显示打印信息
u8 eeprom_read(u8 page, u8 debug_info)
{
    uint8_t EEPROM_READ_BUF[EEPROM_PAGE_SIZE] = {0};
    
    if ((i2c_memory_read(&hi2cx, I2C_MEM_ADDR_WIDIH_16, I2Cx_ADDRESS, page * EEPROM_PAGE_SIZE, EEPROM_READ_BUF, 64, I2C_TIMEOUT)) != I2C_OK)
    {
        logPrintln("error read");
        return FALSE;
    }
    else
    {
        logPrintln("right read = %d", page);
        if (debug_info)
        {
            for (int i = 0; i < EEPROM_PAGE_SIZE; i++)
            {
                logPrintln("right read i:%d,%X", i, EEPROM_READ_BUF[i]);
            }
        }

        return TRUE;
    }
}

/// @brief 读取EEPROM
/// @param page
u8 eeprom_write(u8 page)
{
    uint8_t EEPROM_WRITE_BUF[EEPROM_PAGE_SIZE] = {0};
   

    for (int i = 0; i < EEPROM_PAGE_SIZE; i++)
    {
        logPrintln("ready to write i:%d,%X", i, EEPROM_WRITE_BUF[i]);
    }
    if ((i2c_memory_write(&hi2cx, I2C_MEM_ADDR_WIDIH_16, I2Cx_ADDRESS, page * EEPROM_PAGE_SIZE, EEPROM_WRITE_BUF, 64, I2C_TIMEOUT)) != I2C_OK)
    {
        // error_handler(i2c_status);
        logPrintln("error write");
        return FALSE;
    }
    else
    {
        logPrintln("right write");
        return TRUE;
        // 机械臂数据占用前8页 0-7 page
    }
}
